package com.xb.p8_contextmenu;

import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.provider.ContactsContract;
import android.view.ContextMenu;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        ImageView imageView=findViewById(R.id.image_ting);

        //注册上下文菜单到那个视图上
        registerForContextMenu(imageView);
    }

    //弹出上下文菜单
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        //填充
        getMenuInflater().inflate(R.menu.menu_main,menu);

        //使用代码添加菜单项
        menu.add(0,3,0,"复制");

        super.onCreateContextMenu(menu, v, menuInfo);
    }

    //事件处理
    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        switch(item.getItemId()){
            case R.id.action_setting:
                Toast.makeText(this,"点击了yes菜单选项",Toast.LENGTH_LONG).show();
                break;
            case R.id.action_file:
                Toast.makeText(this,"点击了NO菜单选项",Toast.LENGTH_LONG).show();
                break;
            case 3:
                Toast.makeText(this,"点击了‘复制’菜单选项",Toast.LENGTH_LONG).show();
                break;
        }
        return super.onContextItemSelected(item);
    }
}
